package de.lot.action;

import de.lot.model.Position;
import de.lot.model.Student;
import de.lot.model.StudentLearningOutcome;
import de.lot.model.Trait;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

/**
 * Class to interact between UI and back-end
 *
 * @author Stefan Kohler <kohler.stefan@gmail.com>
 */
@Name("studentLearningOutcomeHome")
public class StudentLearningOutcomeHome extends EntityHome<StudentLearningOutcome> {

    @In(create = true)
    private StudentHome studentHome;
    // Not required because it would otherwise break the application when trying
    // to execute the wire()-method
    @In(create = true, required = false)
    private LearningOutcomeHome learningOutcomeHome;
    @In(create = true)
    private PositionHome positionHome;

    /**
     * Associates a student to the studentLearningOutcome
     */
    public void wire() {
        getInstance();
        if (learningOutcomeHome.getDefinedInstance() != null && getPositions().isEmpty()) {
            for (Trait trait : learningOutcomeHome.getDefinedInstance().getTraitList()) {
                addTraitAsPosition(trait);
            }
        }
        Student student = studentHome.getDefinedInstance();
        if (student != null) {
            getInstance().setStudent(student);
        }
    }

    //TODO: Add Method to saveOrUpdate the changed Positions in
    // getPositions()
    /**
     * Adds the trait that belongs to a position and saves the data.
     * @param trait -
     */
    public void addTraitAsPosition(Trait trait) {
        Position position = new Position(trait, getInstance());
        positionHome.setInstance(position);
        positionHome.persist();
        getInstance().getPositions().add(position);
    }

    /**
     * @return <code>true</code> if studentLearningOutcome is associated with a
     *         student
     *         <code>false</code> if studentLearningOutcome is NOT associated
     *         with a student
     */
    public boolean isWired() {
        if (getInstance().getStudent() == null) {
            return false;
        }
        return true;
    }

    /**
     * @return ArrayList of positions
     */
    public ArrayList<Position> getPositions() {

        if (getInstance() == null) {
            return null;
        }
        List<Position> positions = new ArrayList<Position>(getInstance().getPositions());
        Collections.sort(positions, new Comparator<Position>() {

            public int compare(Position a, Position b) {
                return Integer.valueOf(a.getTrait().getNumber().compareTo(Integer.valueOf(b.getTrait().getNumber())));
            }
        });
        return new ArrayList<Position>(positions);
    }

    public StudentLearningOutcome getDefinedInstance() {
        return isIdDefined() ? getInstance() : null;
    }

    /**
     * @return the studentLearningOutcomeId
     */
    public Long getStudentLearningOutcomeId() {
        return (Long) getId();
    }

    /**
     * @param studentLearningOutcomeId the studentLearningOutcomeId to set
     */
    public void setStudentLearningOutcomeId(Long studentLearningOutcomeId) {
        setId(studentLearningOutcomeId);
    }

    /**
     * @return the studentHome
     */
    public StudentHome getStudentHome() {
        return studentHome;
    }

    /**
     * @param studentHome the studentHome to set
     */
    public void setStudentHome(StudentHome studentHome) {
        this.studentHome = studentHome;
    }
}
